# Figure replication file for "The Determinants of Uncertainty in International Relations." International Studies Quarterly.
# Jeffrey M. Kaplow, William & Mary, jkaplow@wm.edu
# Erik Gartzke, University of California San Diego, egartzke@ucsd.edu

# The code in this file reproduces the figures from Jeffrey M. Kaplow and Erik Gartzke, "The Determinants of Uncertainty
# in International Relations," International Studies Quarterly, using R. For code and data to replicate the quantitative models
# used in the article using Stata, please see DetUnc_Replication.do and the .dta files included in this replication package.

# The code below was created and tested using R version 3.5.2 (2018-12-20)

## Libraries ----
library(Hmisc)      # v4.2-0

## Data file ----
load("DetUnc_Figure_Replication.rdata")

## Figure 1 ----
retro_mean <- vapply(1967:2005, function(i) mean(alldat$retro[which(alldat$year == i)], na.rm=TRUE), 1)
plot(jitter(alldat$year[which(!is.na(alldat$retro) & alldat$coder == 0)]), alldat$retro[which(!is.na(alldat$retro) & alldat$coder == 0)], ylim=c(-.5,.5), pch=19, col="#99999960", ylab="Retrospective uncertainty", xlab="", cex.axis=.75, cex.lab=.75)
points(jitter(alldat$year[which(!is.na(alldat$retro) & alldat$coder == 1)]), alldat$retro[which(!is.na(alldat$retro) & alldat$coder == 1)], pch=17, col="#00000080")
abline(h=0,lty=2,col="#00000080")
lines(c(1967:1978,1980:1983,1986:1993,1998:1999,2004:2005), retro_mean[-c(13,18,19,28:31,34:37)], lwd=3, col="black")


## Figure 2 ----
par(mfcol = c(3, 3))
par(cex = 0.6)
par(mar = c(0, 0, 0, 0), oma = c(5, 5, 5, 0.5))
par(tcl = -0.25)
par(mgp = c(2, 0.6, 0))

# first_pers plots
plot(subeffects$first_pers$x, subeffects$first_pers$pp, type="l", col="black", lwd=4, ylim=c(0, .6), axes=FALSE)
lines(subeffects$first_pers$x, subeffects$first_pers$cil, lty=2, col="black")
lines(subeffects$first_pers$x, subeffects$first_pers$cih, lty=2, col="black")
mtext("All", side = 3, line=-1,adj=.95,padj=1,cex=0.6, col = "Black")
axis(2, col = "grey40", col.axis = "grey20", at = c(0.2,0.4))
box(col = "grey60")

plot(subeffects$first_pers$x, subeffects$first_pers$ppo, type="l", col="grey60", lwd=2, ylim=c(0, .6), axes=FALSE)
lines(subeffects$first_pers$x, subeffects$first_pers$cilo, lty=2, col="grey60")
lines(subeffects$first_pers$x, subeffects$first_pers$ciho, lty=2, col="grey60")
mtext("Over-estimates", side = 3, line=-1,adj=.95,padj=1,cex=0.6, col = "Black")
axis(2, col = "grey40", col.axis = "grey20", at = c(0.2,0.4))
box(col = "grey60")

plot(subeffects$first_pers$x, subeffects$first_pers$ppu, type="l", col="black", lwd=4, ylim=c(0, .6), axes=FALSE)
lines(subeffects$first_pers$x, subeffects$first_pers$cilu, lty=2, col="black")
lines(subeffects$first_pers$x, subeffects$first_pers$cihu, lty=2, col="black")
mtext("Under-estimates", side = 3, line=-1,adj=.95,padj=1,cex=0.6, col = "Black")
axis(2, col = "grey40", col.axis = "grey20", at = c(0.2,0.4))
axis(1, col = "grey40", col.axis = "grey20", at = seq(0, 4000, 2000))
box(col = "grey60")
mtext("Size of armed forces", side = 1, line=2.2,cex=0.7, col = "Black")
mtext("Retrospective uncertainty", side = 2, outer = TRUE, cex = 0.7, line = 2.2,col = "grey20")

# rgdppc plots
plot(subeffects$rgdppc$x, subeffects$rgdppc$pp, type="l", col="black", lwd=4, ylim=c(0, .6), axes=FALSE)
lines(subeffects$rgdppc$x, subeffects$rgdppc$cil, lty=2, col="black")
lines(subeffects$rgdppc$x, subeffects$rgdppc$cih, lty=2, col="black")
mtext("All", side = 3, line=-1,adj=.95,padj=1,cex=0.6, col = "Black")
box(col = "grey60")

plot(subeffects$rgdppc$x, subeffects$rgdppc$ppo, type="l", col="grey60", lwd=2, ylim=c(0, .6), axes=FALSE)
lines(subeffects$rgdppc$x, subeffects$rgdppc$cilo, lty=2, col="grey60")
lines(subeffects$rgdppc$x, subeffects$rgdppc$ciho, lty=2, col="grey60")
mtext("Over-estimates", side = 3, line=-1,adj=.95,padj=1,cex=0.6, col = "Black")
box(col = "grey60")

plot(subeffects$rgdppc$x, subeffects$rgdppc$ppu, type="l", col="grey60", lwd=2, ylim=c(0, .6), axes=FALSE)
lines(subeffects$rgdppc$x, subeffects$rgdppc$cilu, lty=2, col="grey60")
lines(subeffects$rgdppc$x, subeffects$rgdppc$cihu, lty=2, col="grey60")
mtext("Under-estimates", side = 3, line=-1,adj=.95,padj=1,cex=0.6, col = "Black")
axis(1, col = "grey40", col.axis = "grey20", at = c(10000,30000,50000))
box(col = "grey60")
mtext("Real GDP per capita", side = 1, line=2.2,cex=0.7, col = "Black")

# polity2 plots
plot(subeffects$polity2$x, subeffects$polity2$pp, type="l", col="grey60", lwd=2, ylim=c(0, .6), axes=FALSE)
lines(subeffects$polity2$x, subeffects$polity2$cil, lty=2, col="grey60")
lines(subeffects$polity2$x, subeffects$polity2$cih, lty=2, col="grey60")
mtext("All", side = 3, line=-1,adj=.95,padj=1,cex=0.6, col = "Black")
box(col = "grey60")

plot(subeffects$polity2$x, subeffects$polity2$ppo, type="l", col="grey60", lwd=2, ylim=c(0, .6), axes=FALSE)
lines(subeffects$polity2$x, subeffects$polity2$cilo, lty=2, col="grey60")
lines(subeffects$polity2$x, subeffects$polity2$ciho, lty=2, col="grey60")
mtext("Over-estimates", side = 3, line=-1,adj=.95,padj=1,cex=0.6, col = "Black")
box(col = "grey60")

plot(subeffects$polity2$x, subeffects$polity2$ppu, type="l", col="black", lwd=4, ylim=c(0, .6), axes=FALSE)
lines(subeffects$polity2$x, subeffects$polity2$cilu, lty=2, col="black")
lines(subeffects$polity2$x, subeffects$polity2$cihu, lty=2, col="black")
mtext("Under-estimates", side = 3, line=-1,adj=.95,padj=1,cex=0.6, col = "Black")
axis(1, col = "grey40", col.axis = "grey20", at = c(-5,0,5))
box(col = "grey60")
mtext("Regime type", side = 1, line=2.2,cex=0.7, col = "Black")


## Figure 3 ----
par(mfcol=c(2,1))
par(cex = 0.6)
par(mar = c(0, 0, 0, 0), oma = c(5, 0.5, 0.5, 0.5))
par(tcl = -0.25)
par(mgp = c(2, 0.6, 0))
par("lend"=3)

par(xaxt="n")
varnames<-c("Size of armed forces","Real GDP per capita","Nuclear weapons","Regime type","Rivalry","Alliance","Conflict history")

#Between effects
dotchart2(c(NA, subeffects$between$fd[2], NA, subeffects$between$fd[4:7]), labels="",lines=FALSE,cex.labels=.8, main=NA, sort.=FALSE, col="gray80", pch="|", xlab="Change in coder-assessed uncertainty", xlim=c(-.25,.5), reset.par=FALSE, add=FALSE, dotsize=2.5, cex=1, cex.axis=1)
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col=NA,border=NA)

dotchart2(c(NA, subeffects$between$fd[2], NA, subeffects$between$fd[4:7]), labels="", lines=FALSE,cex.labels=.8, main=NA, sort.=FALSE, col="gray80", pch="|", xlab="Change in coder-assessed uncertainty", xlim=c(-.25,.5), reset.par=FALSE, add=TRUE, dotsize=2.5)
dotchart2(c(subeffects$between$fd[1], NA, subeffects$between$fd[3], NA, NA, NA, NA), labels="", lines=FALSE,cex.labels=.8,sort.=FALSE,main=NA,col="black",pch="|",xlab="Change in coder-assessed uncertainty", xlim=c(-.25,.5), reset.par=FALSE, add=TRUE, dotsize=2.5)

linex1<-NA
for(i in 1:nrow(subeffects$between)){
  linex1<-c(linex1,subeffects$between$cil[i],((subeffects$between$cih[i]-subeffects$between$cil[i])/2)+subeffects$between$cil[i],NA)
}
linex1<-linex1[-1]

linex2<-NA
for(i in 1:nrow(subeffects$between)){
  linex2<-c(linex2,((subeffects$between$cih[i]-subeffects$between$cil[i])/2)+subeffects$between$cil[i],subeffects$between$cih[i],NA)
}
linex2<-linex2[-1]

slinex1 <- linex1
slinex2 <- linex2
linex1[c(1:3, 7:9)] <- NA
linex2[c(1:3, 7:9)] <- NA
slinex1[c(4:6, 10:21)] <- NA
slinex2[c(4:6, 10:21)] <- NA

liney<-NA
delta<-(par("usr")[4]-par("usr")[3])/8
ypos<-seq(par("usr")[4]-delta,by=-delta,length=7)
for(i in 1:length(ypos)){
  liney<-c(liney,rep(ypos[i],3))
}
liney<-liney[-1]

lines(linex1,liney,col="gray80",lwd=28)		
lines(linex2,liney,col="gray80",lwd=28)
lines(slinex1,liney,col="black",lwd=28)
lines(slinex2,liney,col="black",lwd=28)

for(i in 1:nrow(subeffects$between)){
  if(i %in% c(2,4:7)) text(subeffects$between$fd[i],liney[3*i],label=varnames[i], col="black", cex=.8)
  if(i %in% c(1,3)) text(subeffects$between$fd[i],liney[3*i],label=varnames[i], col="white", cex=.8)
}
abline(v=0,lty="dotted",col="black",lwd=1)
mtext("Between-country effects", side = 1, line=-1,adj=0.01,padj=-1,cex=1, col = "Black")

#Within effects
dotchart2(c(subeffects$within$fd[1:2], NA, NA, subeffects$within$fd[5], NA, NA), labels="",lines=FALSE,cex.labels=.8, main=NA, sort.=FALSE, col="gray80", pch="|", xlab="Change in coder-assessed uncertainty", xlim=c(-.25,.5), reset.par=FALSE, add=FALSE, dotsize=2.5, cex=1, cex.axis=1)
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col=NA,border=NA)

dotchart2(c(subeffects$within$fd[1:2], NA, NA, subeffects$within$fd[5], NA, NA), labels="", lines=FALSE,cex.labels=.8, main=NA, sort.=FALSE, col="gray80", pch="|", xlab="Change in coder-assessed uncertainty", xlim=c(-.25,.5), reset.par=FALSE, add=TRUE, dotsize=2.5)
dotchart2(c(NA, NA, subeffects$within$fd[3:4], NA, subeffects$within$fd[6:7]), labels="", lines=FALSE,cex.labels=.8,sort.=FALSE,main=NA,col="black",pch="|",xlab="Change in coder-assessed uncertainty", xlim=c(-.25,.5), reset.par=FALSE, add=TRUE, dotsize=2.5)

linex1<-NA
for(i in 1:nrow(subeffects$within)){
  linex1<-c(linex1,subeffects$within$cil[i],((subeffects$within$cih[i]-subeffects$within$cil[i])/2)+subeffects$within$cil[i],NA)
}
linex1<-linex1[-1]

linex2<-NA
for(i in 1:nrow(subeffects$within)){
  linex2<-c(linex2,((subeffects$within$cih[i]-subeffects$within$cil[i])/2)+subeffects$within$cil[i],subeffects$within$cih[i],NA)
}
linex2<-linex2[-1]

slinex1 <- linex1
slinex2 <- linex2
linex1[c(7:12, 16:21)] <- NA
linex2[c(7:12, 16:21)] <- NA
slinex1[c(1:6, 13:15)] <- NA
slinex2[c(1:6, 13:15)] <- NA

liney<-NA
delta<-(par("usr")[4]-par("usr")[3])/8
ypos<-seq(par("usr")[4]-delta,by=-delta,length=7)
for(i in 1:length(ypos)){
  liney<-c(liney,rep(ypos[i],3))
}
liney<-liney[-1]

lines(linex1,liney,col="gray80",lwd=28)		
lines(linex2,liney,col="gray80",lwd=28)
lines(slinex1,liney,col="black",lwd=28)
lines(slinex2,liney,col="black",lwd=28)

for(i in 1:nrow(subeffects$within)){
  if(i %in% c(1:2,5)) text(subeffects$within$fd[i],liney[3*i],label=varnames[i], col="black", cex=.8)
  if(i %in% c(3:4,6:7)) text(subeffects$within$fd[i],liney[3*i],label=varnames[i], col="white", cex=.8)
}
abline(v=0,lty="dotted",col="black",lwd=1)
mtext("Within-country effects", side = 1, line=-1,adj=0.01,padj=-1,cex=1, col = "Black")
par(xaxt="s")
axis(1, col = "grey40", col.axis = "grey20", at = seq(-.2,.5,by=.1), cex.axis=.8)